﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Waiter.Common;

namespace Waiter.Controler
{
    class BillCtrl
    {
        public ResultStatus fsInsertBill(BILL item)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.BILLs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "BillCtrl.fsInsertBill", 
                    CommonUtils.fsObjectToString(item), 
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsUpdateStatus(string code, int status, string note)
        {
            try
            {
                BILL item = CommonGlobalVariable.globalDataContext.BILLs.First(q => q.CODE == code);
                item.STATUS_CODE = status;
                item.NOTE = note;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "BillCtrl.fsUpdateStatus",
                    CommonUtils.fsParamListToString(new object[3]{code, status, note}),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public BILL fsFindBillBycode(string code)
        {
            try
            {
                return CommonGlobalVariable.globalDataContext.BILLs.FirstOrDefault(q => q.CODE == code);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "BillCtrl.fsFindBillBycode",
                    CommonUtils.fsParamListToString(new object[]{code}),
                    ex.ToString());
            }

            return null;
        }
    }
}
